#MP Moralization and Foreign Policy Attitudes Replication File
# This file contains all code necessary to replicate pre-test results reported Appendix Section C.2
### Last updated: 10 June 2025

###Analyses carried out using R version 4.4.3 in RStudio version 2024.04.2+764 on Lenovo ThinkPad X1 Carbon Gen 12 Intel Core Ultra 7 155U, 1700 Mhz, 12 running Windows 11 Enterprize

#Run all code in the order that it appears! 

#Uncomment to install packages if needed:
#install.packages("ggplot2")
#install.packages("plyr")

library(foreign)
library(ggplot2)
library(plyr)



#Load the data
setwd("") #Set your working directory to the file containing the data
data <- read.csv("MP_study2_pretest_data.csv")

colnames(data)

#Screener
round(prop.table(table(data$screener1))*100, digits=2) # 86.09% correctly select the first two options on the screener

data$screenercorrect <- ifelse(data$screener1=="1,2", 1, 0)

#drop participants who failed screener
x <- which(data$screenercorrect==0)
data <- data[-x,] #718




# Variable coding functions ---------------------------------------------------------

#Function to rescale variables to range from 0 to 1
rescale <- function(x){
  return((x-min(x,na.rm=TRUE))/(max(x-min(x,na.rm=TRUE),na.rm=TRUE)))
}

#Function to recode variables
recode <- function(variable, reverse=FALSE, maxVal, minVal, binarize=FALSE, x=NA, x2){
  if (is.factor(variable)){variable <- as.numeric(variable)}
  if (missing(maxVal)){maxVal <- max(variable, na.rm=TRUE)}
  if (missing(minVal)){minVal <- min(variable, na.rm=TRUE)}
  variable[variable > maxVal] <- NA
  variable[variable < minVal] <- NA
  if (reverse){
    temp <- variable
    for (j in 1:maxVal){
      temp[which(variable==j)] <- maxVal-j+1
    }
    return(temp)
  }
  if (binarize){
    if (is.na(x)){stop("Specify the value to dichotomize on")}
    temp <- variable
    if (missing(x2)){
      i <- which(variable == x)
      k <- which(variable < x | variable > x)
    }
    else{
      i <- which(variable >= x & variable <= x2)
      k <- which(variable < x | variable > x2)
    }
    temp[i] <- 1
    temp[k] <- 0
    return(temp)
  }
  return(variable)
}


#Function to organize summary data:
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}



# ###Variable coding ------------------------------------------------------

#Cost treatments

#Vaccine aid: 
#Treatment: The analysis also concluded that the program is very [in]expensive. It will cost [$760,000]$230 million dollars, which is about [$1]$300 per individual aided. 

data$aidtreat <- ifelse(data$aidcost=="$300 per person aided, for a total of $230 million dollars", "highaid",
                        ifelse(data$aidcost=="$1 per person aided, for a total of $760,000 dollars", "lowaid", NA))

#Nuclear:
# Treatment: "The analysis also concluded that, compared to other recent policies, sanctions would be very [in]expensive for the U.S. and American consumers."
data$nuketreat <- ifelse((data$securityprogram=="to convince them to stop buying or selling maraging steel" & data$sanctioncost=="expensive"), "highnuke",
                         ifelse((data$securityprogram=="to convince them to stop buying or selling maraging steel" & data$sanctioncost=="inexpensive"), "lownuke", NA))

#Counter-terrorism:
# Treatment: "The analysis also concluded that, compared to other recent policies, sanctions would be very [in]expensive for the U.S. and American consumers."
data$terrtreat <- ifelse((data$securityprogram=="to convince them to combat terrorist groups" & data$sanctioncost=="expensive"), "highterr",
                         ifelse((data$securityprogram=="to convince them to combat terrorist groups" & data$sanctioncost=="inexpensive"), "lowterr", NA))


##Dummy indicator for type of security program (nuclear or counter-terrorism)
data$secissue <- ifelse(data$securityprogram=="to convince them to stop buying or selling maraging steel" , "nuke",
                        ifelse(data$securityprogram=="to convince them to combat terrorist groups", "terror", NA))


#format as factor with "inexpensive" as reference group
data$sanctioncost <- as.factor(data$sanctioncost)
data$sanctioncost <- relevel(data$sanctioncost, ref="inexpensive")

data$aidtreat <- as.factor(data$aidtreat)
data$aidtreat <- relevel(data$aidtreat, ref="lowaid")


##Post-treatment cost manipulation checks:

#"In the scenario you read earlier, the global vaccine readiness program cost ${e://Field/aidcost}. In your opinion, how expensive is this program for the U.S.?"
data$aid_expense1 <- rescale(data$aid_expense) # higher values = "very expensive" for the U.S.


#"Earlier, you read about the U.S. imposing economic sanctions on another country ${e://Field/securityprogram}. In your opinion would the sanctions be:"
data$security_expenser <- ifelse(data$security_expense==1, 1, 
                                 ifelse(data$security_expense==2, 0, NA)) # 1= "very costly for American consumers"; 0="not at all costly for American consumers"


#Cost of sanctions 
# "Earlier, you read that if the U.S. imposed economic sanctions on another country ${e://Field/securityprogram}, 
# experts expected the sanctions would be very ${e://Field/sanctioncost} for American consumers.
# In your opinion, if the U.S. imposes these sanctions, how likely are each of the following consequences:"

#Higher values = "very likely" that sanctions will create the consequence
data$sanctionscon_tax <- rescale(data$sanctionscon_1) # your taxes will increase
data$sanctionscon_food <- rescale(data$sanctionscon_2) # food prices will increase
data$sanctionscon_deficit <- rescale(data$sanctionscon_3) # U.S. budget deficit will increase
data$sanctionscon_col <- rescale(data$sanctionscon_4) # cost of living in U.S. will increase
data$sanctionscon_personal <- rescale(recode(data$sanctionscon_5, reverse = TRUE)) # personal daily expenses will NOT change; reverse coded
data$sanctionscon_jobs <- rescale(data$sanctionscon_6) # workers in the U.S. will lose their jobs


#Escalating expense
#"U.S. foreign policy programs have different costs for the government. Some programs cost about $1 per person served, whereas others require more resources per person served.
# All things considered, please tell us whether you consider each of the following amounts expensive or inexpensive when evaluating a foreign policy program." 

#renaming for clarity - higher values + $N per person saved is "very expensive"
names(data)[names(data) == "genexpense_1"] <- "genexpense1" #$1 per person saved
names(data)[names(data) == "genexpense_2"] <- "genexpense10" #$10 per person saved
names(data)[names(data) == "genexpense_3"] <- "genexpense100" #$100 per person saved
names(data)[names(data) == "genexpense_4"] <- "genexpense500" #$500 per person saved
names(data)[names(data) == "genexpense_5"] <- "genexpense1000" #$1000 per person saved
names(data)[names(data) == "genexpense_6"] <- "genexpense5000" #$5000 per person saved

data$genexpense1_1 <- rescale(data$genexpense1)
data$genexpense10_1 <- rescale(data$genexpense10)
data$genexpense100_1 <- rescale(data$genexpense100)
data$genexpense500_1 <- rescale(data$genexpense500)
data$genexpense1000_1 <- rescale(data$genexpense1000)
data$genexpense5000_1 <- rescale(data$genexpense5000)



# Manuscript Footnote 26: Effect of high cost sanction treatment ----------

#The high cost sanction treatment increased participant beliefs that their taxes, cost of living, and food expenses would rise:

mod.sanccost.tax.1 <- lm(sanctionscon_tax ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.tax.1) #b=0.14, p<0.01; high cost increases belief that taxes will increase

mod.sanccost.food.1 <- lm(sanctionscon_food ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.food.1) #b=0.18, p<0.01; high cost increases belief that cost of food will increase

mod.sanccost.col.1 <- lm(sanctionscon_col ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.col.1) #b=0.15, p<0.01; high cost increases belief that cost of living will increase

#Not mentioned in footnote: 
mod.sanccost.def.1 <- lm(sanctionscon_deficit ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.def.1)#b=0.13, p<0.01; high cost increases belief that US deficit will increase

mod.sanccost.pers.1 <- lm(sanctionscon_personal ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.pers.1) #b=0.08, p<0.05; high cost increases believe that personal daily expenses will increase

mod.sanccost.jobs.1 <- lm(sanctionscon_jobs ~ sanctioncost*secissue, data=data)
summary(mod.sanccost.jobs.1) #b=0.12, p<0.01; high cost increases believe that U.S. workers will lose their jobs



# ### Appendix Section C.2: Manipulating Costs ----------------------------

#Pre-test data showed that participants rate programs that cost $1,000-$5,000 per individual aided as subjectively quite expensive, 
#but see those that cost $1-5 per individual as subjectively inexpensive

#Subset the data to remove NAs:
dat.aid <- subset(data, !is.na(data$aidtreat))

#Summary data for each amount: 
gen1.sum <- summarySE(dat.aid, measurevar="genexpense1_1", groupvars = "aidtreat", na.rm=TRUE)
gen10.sum <- summarySE(dat.aid, measurevar="genexpense10_1", groupvars = "aidtreat", na.rm=TRUE)
gen100.sum <- summarySE(dat.aid, measurevar="genexpense100_1", groupvars = "aidtreat", na.rm=TRUE)
gen500.sum <- summarySE(dat.aid, measurevar="genexpense500_1", groupvars = "aidtreat", na.rm=TRUE)
gen1000.sum <- summarySE(dat.aid, measurevar="genexpense1000_1", groupvars = "aidtreat", na.rm=TRUE)
gen5000.sum <- summarySE(dat.aid, measurevar="genexpense5000_1", groupvars = "aidtreat", na.rm=TRUE)


#Combine into a data frame: 
genmeans.mat <- data.frame(rbind(cbind(gen1.sum[1,3], (gen1.sum[1,3]-gen1.sum[1,6]), (gen1.sum[1,3]+gen1.sum[1,6])),
                                 cbind(gen1.sum[2,3], (gen1.sum[2,3]-gen1.sum[2,6]), (gen1.sum[2,3]+gen1.sum[2,6])),
                                 cbind(gen10.sum[1,3], (gen10.sum[1,3]-gen10.sum[1,6]), (gen10.sum[1,3]+gen10.sum[1,6])),
                                 cbind(gen10.sum[2,3], (gen10.sum[2,3]-gen10.sum[2,6]), (gen10.sum[2,3]+gen10.sum[2,6])),
                                 cbind(gen100.sum[1,3], (gen100.sum[1,3]-gen100.sum[1,6]), (gen100.sum[1,3]+gen100.sum[1,6])),
                                 cbind(gen100.sum[2,3], (gen100.sum[2,3]-gen100.sum[2,6]), (gen100.sum[2,3]+gen100.sum[2,6])),
                                 cbind(gen500.sum[1,3], (gen500.sum[1,3]-gen500.sum[1,6]), (gen500.sum[1,3]+gen500.sum[1,6])),
                                 cbind(gen500.sum[2,3], (gen500.sum[2,3]-gen500.sum[2,6]), (gen500.sum[2,3]+gen500.sum[2,6])),
                                 cbind(gen1000.sum[1,3], (gen1000.sum[1,3]-gen1000.sum[1,6]), (gen1000.sum[1,3]+gen1000.sum[1,6])),
                                 cbind(gen1000.sum[2,3], (gen1000.sum[2,3]-gen1000.sum[2,6]), (gen1000.sum[2,3]+gen1000.sum[2,6])),
                                 cbind(gen5000.sum[1,3], (gen5000.sum[1,3]-gen5000.sum[1,6]), (gen5000.sum[1,3]+gen5000.sum[1,6])),
                                 cbind(gen5000.sum[2,3], (gen5000.sum[2,3]-gen5000.sum[2,6]), (gen5000.sum[2,3]+gen5000.sum[2,6]))))
genmeans.mat <- cbind(genmeans.mat, treat = rep(c("High Cost", "Low Cost"), times=6),
                      cost=rep(c("$1", "$10", "$100", "$500", "$1000", "$5000"), each=2))

colnames(genmeans.mat) <- c("Y", "Low_95", "Hi_95", "Treatment","Cost")  
genmeans.mat$Y <- as.numeric(as.character(genmeans.mat$Y))
genmeans.mat$Low_95 <- as.numeric(as.character(genmeans.mat$Low_95))
genmeans.mat$Hi_95 <- as.numeric(as.character(genmeans.mat$Hi_95))

#Order for plotting: 
genmeans.mat$Cost <- factor(genmeans.mat$Cost, levels = c("$1", "$10", "$100", "$500", "$1000", "$5000")) 


#Generate visualization: 
qplot(Cost, Y, data=genmeans.mat, ymin=Low_95, ymax=Hi_95, ylab="Expensive (95% CI)", xlab="", geom="pointrange", 
      colour=factor(Treatment), size=I(1.2)) + facet_wrap(~Treatment, ncol=3) + 
  theme_bw() + #scale_colour_manual(values=c("black","dodgerblue4", "deepskyblue")) +
  theme(text = element_text(size = 18), axis.text.x=element_text(color="black", size=16), 
        axis.text.y=element_text(color="black", size=18),
        legend.position = "none")


#Pre-test data describing the 'very expensive' program as costing $230 million showed that the treatment significantly 
# affected participating views about whether the program was costly for the U.S.:

mod.vaxcost.1 <- lm(aid_expense1 ~ aidtreat, data=data)
summary(mod.vaxcost.1) #b=0.34, p<0.01


